package org.example;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class ImageProcessingGUI extends JFrame {

    private static String imagePath;  // 待处理图片路径

    public static void GUI() {

        JFrame frame = new JFrame("图像处理");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1300, 650);

        JPanel panel = new JPanel();
        panel.setLayout(null);

        JButton uploadButton = new JButton("上传照片");
        uploadButton.setBounds(10, 10, 200, 30);
        JButton cartoonizeButton = new JButton("动漫化");
        cartoonizeButton.setBounds(220, 10, 200, 30);
        JButton intensifierButton = new JButton("图像清晰度增强");
        intensifierButton.setBounds(430, 10, 200, 30);
        JLabel inputImageLabel = new JLabel();
        inputImageLabel.setBounds(0, 80, 600, 500);
        JLabel outputImageLabel = new JLabel();
        outputImageLabel.setBounds(630, 80, 600, 500);


        uploadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    ImageIcon icon = null;
                    try {
                        imagePath = selectedFile.getAbsolutePath();
                        System.out.println("the new image path is " + imagePath);
                        icon = new ImageIcon(ImageIO.read(new File(imagePath)));
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    inputImageLabel.setIcon(icon);
                    outputImageLabel.setIcon(null);
                }
            }
        });

        cartoonizeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String imagePath2 = imagePath;
                    System.out.println("imagePath2 : " + imagePath2);
                    String newImagePath = ImageProcessingSDK.imageProcess(imagePath2);
                    ImageIcon cartoonIcon = new ImageIcon(ImageIO.read(new File(newImagePath)));
                    outputImageLabel.setIcon(null);
                    outputImageLabel.setIcon(cartoonIcon);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        intensifierButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String imagePath2 = imagePath;
                    System.out.println("imagePath2 : " + imagePath2);
                    String newImagePath = ImageProcessingSDK.imageEnhancement(imagePath2);
                    ImageIcon cartoonIcon = new ImageIcon(ImageIO.read(new File(newImagePath)));
                    outputImageLabel.setIcon(null);
                    outputImageLabel.setIcon(cartoonIcon);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        panel.add(uploadButton);
        panel.add(intensifierButton);
        panel.add(inputImageLabel);
        panel.add(cartoonizeButton);
        panel.add(outputImageLabel);
        frame.add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        GUI();
    }
}

